//
//  HECircleCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/10.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HECircleCell.h"
#import "HEChatRoomModel.h"

@interface HECircleCell ()

@property (nonatomic, strong) UIImageView *teamIcon;
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *descLb;
@property (nonatomic, strong) UIView *userWarp;
@property (nonatomic, strong) NSMutableArray *iconsArr;
@property (nonatomic, assign) CGFloat itemWidth;

@property (nonatomic, strong) HEChatRoomModel *model;

@end

@implementation HECircleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = CViewBgColor;
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
     
    UIView *warp = [UIView new];
    warp.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    warp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    warp.layer.shadowOffset = CGSizeMake(0,5);
    warp.layer.shadowOpacity = 1;
    warp.layer.shadowRadius = 8;
    warp.layer.cornerRadius = 8;
    [self.contentView addSubview:warp];

    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.height.mas_greaterThanOrEqualTo(80);
        make.bottom.equalTo(self.contentView).mas_offset(-20);
    }];
    
    UIView *userWarp = [UIView new];
//    userWarp.backgroundColor = CViewBgColor;
    _userWarp = userWarp;
    [warp addSubview:userWarp];
    
    _teamIcon = [UIImageView new];
    _teamIcon.layer.masksToBounds = YES;
    _teamIcon.layer.cornerRadius = 30;
    [warp addSubview:_teamIcon];
    
    _titleLb = [UILabel new];
    _titleLb.text = @"-";
    _titleLb.font = DYBlodFont(17);
    [warp addSubview:_titleLb];
    
    _descLb = [UILabel new];
    _descLb.numberOfLines = 0;
    _descLb.text = @"3人在线";
    _descLb.font = DYFont(15);
    _descLb.textColor = CNavDargGrayColor;
    [warp addSubview:_descLb];

    [_teamIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(warp);
        make.width.height.offset(60);
        make.left.equalTo(warp).offset(10);
    }];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).offset(15);
        make.left.equalTo(_teamIcon.mas_right).offset(10);
        make.right.equalTo(warp).offset(-15);
    }];
    [_titleLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    
    CGFloat warpWidth = KScreenWidth / 2.0 - 20 - 60 - 10;
    _itemWidth = [self getItemWidthWithWarpWidth:warpWidth num:5 leftMargin:0 isCenter:YES];
    
    [userWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(10);
        make.left.equalTo(_teamIcon.mas_right).offset(10);
        make.right.equalTo(warp);
        make.height.offset(_itemWidth);
    }];
    [userWarp hg_setCornerOnLeftWithRadius:_itemWidth / 2.0];

    [_descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userWarp.mas_bottom).offset(10);
        make.left.equalTo(_teamIcon.mas_right).offset(10);
        make.right.equalTo(warp).offset(-15);
        make.bottom.equalTo(warp).offset(-15);
    }];
    
    [self createUserIcons];
}

- (CGFloat)getItemWidthWithWarpWidth:(CGFloat)warpWidth num:(NSInteger)num leftMargin:(CGFloat) leftMargin isCenter:(BOOL)isCenter {
    if (isCenter) {
        return (2 * warpWidth / (num + 1));
    }
    return (warpWidth + (num - 1) * leftMargin) / num;
}

- (void)createUserIcons {
    
    _iconsArr = @[].mutableCopy;
    CGFloat itemWidth = _itemWidth - 4;
    
    for (int i = 0; i < 5; i++) {
        UIImageView *iconView = [UIImageView new];
        iconView.layer.borderWidth = 1;
        iconView.layer.borderColor = CViewBgColor.CGColor;
        iconView.layer.masksToBounds = YES;
        iconView.hidden = YES;
        iconView.layer.cornerRadius = itemWidth / 2.0;
        [_userWarp addSubview:iconView];
        [_iconsArr addObject:iconView];
         
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_userWarp);
            if (i == 0) {
                make.left.equalTo(_userWarp).offset(2);
            }else {
                UIImageView *lastView = _iconsArr[i-1];
                make.left.equalTo(lastView.mas_right).offset(-itemWidth/2.0);
            }
            make.width.height.offset(itemWidth);
        }];
    }
}

- (void)configCellWithModel:(HEChatRoomModel *)model indexPath:(NSIndexPath *)indexPath {
    _model = model;
    
    _titleLb.text = model.roomName;
    _descLb.text = model.KroomNumText;
    [_teamIcon sd_setImageWithURL:[NSURL URLWithString: model.roomIcon] placeholderImage:IMAGE_NAMED(@"defualt_icon")];

    [self updateTeamInfo: model];
}
 
- (void)updateTeamInfo:(HEChatRoomModel *)model {

    // 更新用户头像列表
    [self.iconsArr enumerateObjectsUsingBlock:^(UIImageView *  _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < model.users.count) {
            view.hidden = NO;
            NSString *iconStr = [DYTools isBlankString:model.users[idx]] ? @"" : model.users[idx];
            [view sd_setImageWithURL:[NSURL URLWithString: iconStr] placeholderImage:[UIImage imageNamed:@"defualt_icon"]];
        }else {
            view.image = nil;
            view.hidden = YES;
        }
    }];
}

@end
